<?php
require("simple_html_dom.php");
include "config.php";
Requests::register_autoloader();
header('Content-type: application/json');

if (isset($_GET["uid"])){
    $UID = $_GET["uid"];
} else {
    echo json_encode(array());
    die(0);
}

if (isset($_GET["page"])){
    $page = $_GET["page"];
} else {
    $page = "1";
}

$url = "http://www.jianshu.com/users/". $UID ."/notebooks?slug=". $UID ."&type=manager&page=". $page ."&pre_page=5";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$response = curl_exec($ch);
$data = json_decode($response, true);
$r = array(
    "notebooks" => array(),
    "page" => $data["page"],
    "total_pages" => $data["total_pages"]
);
foreach ($data["notebooks"] as $notebook){
    array_push($r["notebooks"], array(
        "id" => $notebook["id"],
        "name" => $notebook["name"],
        "lsUrl" => $HOST."article_list.php?page=1&type=nb&id=".$notebook["id"]
    ));
}

echo json_encode($r);

?>